//
//  NetworkConfig.m
//  BaseProject
//
//  Created by Macx on 2018/2/22.
//  Copyright © 2018年 Gavin. All rights reserved.
//

#import "NetworkConfig.h"

@implementation NetworkConfig

+ (NetworkConfig *)sharedConfig {
    
    static NetworkConfig *sharedInstance = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
        sharedInstance.language = @"en-US,en";
    });
    
    return sharedInstance;
}

-(NSString *)areaCode
{
    if(!_areaCode){
        return [UserDefaults objectForKey:kAreacode];
    }
    return _areaCode;
}

//单向验证
- (AFSecurityPolicy *)securityPolicy{
    //设置证书模式，AFSSLPinningModeNone，代表前端包内不验证
    //在单向认证时，前端不放证书，服务器去验证
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    // 如果是需要服务端验证证书，需要设置为YES
    securityPolicy.allowInvalidCertificates = YES;
    //validatesDomainName 是否需要验证域名，默认为YES；
    securityPolicy.validatesDomainName = NO;
    return securityPolicy;
}

//双向验证
- (AFSecurityPolicy *)otherSecurityPolicy{
    //设置验证证书，该模式下需要把证书打包进项目里，进行验证
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    //证书的路径
    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"xxx" ofType:@".cer"];
    NSData *dataSou = [NSData dataWithContentsOfFile:cerPath];
    NSSet *set = [NSSet setWithObjects:dataSou, nil];
    securityPolicy.allowInvalidCertificates = YES;
    securityPolicy.validatesDomainName = YES;
    [securityPolicy setPinnedCertificates:set];
     return securityPolicy;
}

- (NSURLSessionConfiguration *)sessionConfiguration{
    return [NSURLSessionConfiguration defaultSessionConfiguration];
}

///传给后台的语言
-(void)setLanguage:(NSString *)language
{
    if([language containsString:@"zh"]){
        _language = @"zh-CN,zh";
    }else if([language containsString:@"en"]){
        _language = @"en-US,en";
    }else if([language containsString:@"fr"]){//法语
        _language = @"fr-FR,fran";
    }else if([language containsString:@"de"]){//德语
        _language = @"de-DE,de";
    }else if([language containsString:@"ja"]){//日语
        _language = @"ja-JP,jan";
    }else if([language containsString:@"it"]){//意大利语
        _language = @"en-US,en";
    }else if([language containsString:@"es"]){//西班牙语
        _language = @"es-ES,es";
    }else if([language containsString:@"pl"]){//波兰语
        _language = @"pl-PL,pl";
    }
    else{
        _language = @"en-US,en";
    }
}

@end
